<?php
// 模块名_控制名
class Warehouse_PrintLogisticsController extends Zend_Controller_Action {

    public function preDispatch() {
        $this->tplDirectory = "warehouse/logisticsprint/"; // 控制对应模板目录
    }

    public function indexAction() {
        $test = trim($this->_request->getParam ( 'test', 'no' ));
        if($test!="yes"){
            echo Cff::renderTpl ( $this->tplDirectory . "index.tpl", "layout" );
        }else{
            echo Cff::renderTpl ( $this->tplDirectory . "test.tpl", "layout" );

        }
    }
    /**
     * 订单产品
     * Param orders_code
     */
    public function orderProductAction() {
        $data = array ("ask" => 0 );
        $orders_code = $this->_request->getParam ( 'orders_code', '' );
        $row_order = Order_Service_Order::getByOrdersCode ( $orders_code );

        $data ['order'] = $row_order;
        try {

            if ($row_order) {
/*                if($row_order['orders_print_type']<1){
                    throw new Exception ( "订单为一票一件，不可在此操作" );
                }*/
                if (in_array($row_order ['process_status'],array(2,3))) {

                    //验证订单产品与配货产品是否一致，包括产品数和产品名称
                    $validate = Order_Service_OrderProccess::validatePickupMap($orders_code);
                    if($validate['ask']==0){
                        throw new Exception ( "配货异常".$validate['msg'] );
                    }

                    $customer = Customer_Service_Customer::getByCustomerId($row_order['customer_id']);
                    $data ['customer'] = $customer;
                    $data ['not_check_label'] = $customer['not_check_label'];
                    $order_ship = Order_Service_OrderShip::getLastRowByOrdersCode ( $orders_code );
                    $order_row = array ();
                    if (! $order_ship) {

                            $data ['tracking_number'] = $orders_code;

                    } else {

                        $data ['tracking_number'] = $order_ship ['tracking_number'];

                    }

// 					// 修改订单状态 该处禁用 调至 /order/order/print-pop/
// 					$order_row ['process_status'] = 4;
// 					if(!Order_Service_Order::update ( $order_row, $row_order ['order_id'] )){
// 						throw new Exception ( "更新订单状态失败" );
// 					}


                    $products = Order_Service_Product::getByfield ( "orders_code", $orders_code );
                    if ($products) {
                        $data ['ask'] = 1;
                        foreach ( $products as $k => $v ) {
                            $products [$k] ['pic'] = "/product/product/view-picture/id/" . ($v ["product_id"]);

// 							$products [$k] ['pic'] = Product_Service_Receiving::getPicturesPath($v['product_id']);
                            $product = Product_Service_Product::getByProductId ( $v ["product_id"] );
                            $products [$k] ['product'] = $product;
                            $products [$k] ['product_id'] = $product ['product_id'];
                            $products [$k] ['title'] = $product ['product_title'];
                            $products [$k] ['customer_id'] = $product ['customer_id'];
                            $products [$k] ['title_cn'] = $product ['product_title_cn'];

                            $products [$k] ['pp_name'] = "";

                            $row_PackageMap = Product_Service_PackageMap::getAllByProductId ( $v ['product_id'] );

                            if($row_PackageMap){
                                $row_package = Product_Service_Package::getByPpid ( $row_PackageMap ['package_id'] );
                                $products [$k] ['pp_name'] = $row_package['pp_name'];
                                $data['pp_name'] = $row_package['pp_name'];
                            }



                        }
                        $data ['products'] = $products;

                    }else{
                        throw new Exception ( "订单状态无产品" );
                    }
                } else {
                    throw new Exception ( "订单状态不正确" );

                }
            } else {
                throw new Exception ( "订单不存在" );

            }
        } catch ( Exception $e ) {
            $data ['ask'] = 0;
            $data ['msg'] = $e->getMessage ();
        }

        // print_r($date);
        die ( Zend_Json::encode ( $data ) );
    }

}
